package com.jd.org.controller;

import com.jd.basic.util.AjaxsResult;


import com.jd.basic.util.PageList;
import com.jd.org.domain.Employee;
import com.jd.org.query.EmpQuery;
import com.jd.org.service.IEmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门",description = "部门")
@RestController
@RequestMapping("/employee")
public class EmpController {
    @Autowired
    private IEmpService empService;

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "单个查询")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id")Long id){
        return empService.loadById(id);
    }


    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Employee> findAll(){
        return empService.loadAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxsResult remove(@PathVariable("id")Long id){
        try {
            empService.remove(id);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }

    /**
     * 修改和添加
     */

    @ApiOperation(value = "修改和添加")
    @PutMapping
    public AjaxsResult save(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                // 添加
                empService.save(employee);

            }else {
                // 修改
                empService.update(employee);
            }
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"操作失败");
        }
    }

    /**
     * 分页查询
     * PageList<Department>返回参数：需要数据和总条数
     *@PathVariable DepartmentQuery departmentQuery：获取路径的当前页和显示的条数
     */
    @PostMapping // 匹配前端url地址
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmpQuery empQuery){
        return empService.queryPage(empQuery);
    }

    // 批量删除
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxsResult patchRemove(@RequestBody List<Long> ids){
        try {
            empService.patchRemove(ids);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }
    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id")Long shopId){

        return empService.findByShopId(shopId);
    }



}
